package com.xbang.attendance.web.action.purchase.placespread;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.demo2do.lighturl.annotation.Action;
import com.xbang.attendance.entity.companygps.companyGps;
import com.xbang.attendance.entity.department.Department;
import com.xbang.attendance.entity.legworktimes.LegworkTimes;
import com.xbang.attendance.entity.userinfo.Userinfo;
import com.xbang.attendance.entity.vo.LegworkTimesVO;
import com.xbang.attendance.service.SystemService;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

public class Index extends SearchActionSupport {

	private static final long serialVersionUID = 4604709900883572987L;

	@Autowired
	private SystemService systemService;

	private List<Userinfo> userinfos=new ArrayList<Userinfo>(); 
	
	private List<companyGps> companyinfos=new ArrayList<companyGps>(); 
	
	private List<LegworkTimesVO> list=new ArrayList<LegworkTimesVO>();

	private Map<String,Integer> map=new HashMap<String, Integer>();
	
	private List<Department> departList=new ArrayList<Department>();
	
	private List<Integer> effcountList=new ArrayList<Integer>(); 
	
	private String startDate;
	
	private String endDate;
	
	public List<Integer> getEffcountList() {
		return effcountList;
	}

	public void setEffcountList(List<Integer> effcountList) {
		this.effcountList = effcountList;
	}

	public List<Userinfo> getUserinfos() {
		return userinfos;
	}

	public void setUserinfos(List<Userinfo> userinfos) {
		this.userinfos = userinfos;
	}

	public List<companyGps> getCompanyinfos() {
		return companyinfos;
	}

	public void setCompanyinfos(List<companyGps> companyinfos) {
		this.companyinfos = companyinfos;
	}

	public Map<String, Integer> getMap() {
		return map;
	}

	public void setMap(Map<String, Integer> map) {
		this.map = map;
	}

	public List<LegworkTimesVO> getList() {
		return list;
	}

	public void setList(List<LegworkTimesVO> list) {
		this.list = list;
	}

	public List<Department> getDepartList() {
		return departList;
	}

	public void setDepartList(List<Department> departList) {
		this.departList = departList;
	}
	
	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	@Action("/purchase/placespread/list")
	public String list() throws Exception {

		Calendar calendar = Calendar.getInstance();  
		  
		  SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");  
		  
		      //得到月初  
		      calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));  
		      Date dateFrom = calendar.getTime(); 
		  
		      //得到月末  
		      calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));  
		      Date dateTo = calendar.getTime(); 
		    // String a =  strDateTo.substring(0, 5);
		     
		      startDate=fmt.format(dateFrom);
		      endDate=fmt.format(dateTo);
		     
		     queryList(startDate,endDate);		
		     
		     
		     
		return "index";
	}
	
	public void queryList(String startDate ,String endDate) throws Exception{
		
       //查询所有的职员
		String userhql="from Userinfo Userinfo where 1=1";
		Result uResult=systemService.searchByPage(userhql,searchComponent, page);
		userinfos =uResult.getItems();
		
		//根据职员查出对应的部门名称和有效签到总数
		for (int i = 0; i < userinfos.size(); i++) {
			int deid=Integer.valueOf(userinfos.get(i).getDepartment());
			String userid=userinfos.get(i).getUserId();
			Department dp=(Department) systemService.getObject(Department.class, deid);
			departList.add(dp);
			String sql="select count(*) from legwork_times where user_id='"+userid+"' and SUBSTRING(start_time,1,10)>='"+startDate+"' and SUBSTRING(start_time,1,10)<='"+endDate+"'";
			Integer effcount=systemService.queryForInt(sql, page);
			effcountList.add(effcount);
		}
		
		
		
		//查询所有公司
		String companyhql="from companyGps companyGps where 1=1";
		Result cResult=systemService.searchByPage(companyhql,searchComponent, page);
		companyinfos =cResult.getItems();
		
		//根据公司循环所有行
		for (int i = 0; i < companyinfos.size(); i++) {
			//得到公司id
			String companyId=companyinfos.get(i).getCompanyId();
			//创建一个VO类封装公司名称,开始时间，结束时间和List<map>
			LegworkTimesVO ltVO=new LegworkTimesVO();
			
			//封装每一个行中的所有map    map封装的是userName和签到次数
			List<Integer> rowList=new ArrayList<Integer>();
			
			//循环所有的user 构造所有的列
			for (int j = 0; j < userinfos.size(); j++) {
				String userId=userinfos.get(j).getUserId();
				//查询中间表中某个职员在某个外勤公司签到的次数
				String sql="select count(*) from legwork_times where company_id='"+companyId+"' and user_id='"+userId+"' and SUBSTRING(start_time,1,10)>='"+startDate+"' and SUBSTRING(start_time,1,10)<='"+endDate+"'";
				int count=systemService.queryForInt(sql, page);
				rowList.add(count);
			}
			
			//set  LegworkTimesVO的属性值
			ltVO.setCompanyName(companyinfos.get(i).getCompanyName());			
			ltVO.setCountList(rowList);
			ltVO.setStartDate(startDate);
			ltVO.setEndDate(endDate);
			
			//将LegworkTimesVO添加到封装了所有行的list
			list.add(ltVO);
		}
	}
    


	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub
	 }



	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}
		
	}